#include "myopenglwidget.h"

unsigned int VBO,VAO,EBO;


/* 画矩形，用两个三角形合并需要存储6个顶点信息 */
// float vertices[]={
//     //first triangle
//     0.5f,0.5f,0.0f,
//     0.5f,-0.5f,0.0f,
//     -0.5f,0.5f,0.0f,

//     //second triangle
//     0.5f,-0.5f,0.0f,
//     -0.5f,-0.5f,0.0f,
//     -0.5f,0.5f,0.0f,
// };

float vertices[]={
        //position     //RGB          //Texture
        0.5f,0.5f,0.0f,1.0f,0.0f,0.0f,1.0f,1.0f,
        0.5f,-0.5f,0.0f,0.0f,1.0f,0.0f,1.0f,0.0f,
        -0.5f,-0.5f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,
        -0.5f,0.5f,0.0f,0.5f,0.5f,0.5f,0.0f,1.0f,
};

unsigned int indices[]={
    0,1,3,      //第一个三角形
    1,2,3       //第二个三角形
};

MyOpenglWidget::MyOpenglWidget(QWidget *parent):QOpenGLWidget(parent) {

}

MyOpenglWidget::~MyOpenglWidget()
{
    delete textureWall;
    makeCurrent();
    glDeleteVertexArrays(1,&VAO);
    glDeleteBuffers(1,&VBO);
    glDeleteBuffers(1,&EBO);

    doneCurrent();
}

void MyOpenglWidget::drawRectangle()
{
    ShapeType=GL_TRIANGLES;
    isDraw=true;
    update();
}

void MyOpenglWidget::clear()
{
    isDraw=false;
    update();
}

void MyOpenglWidget::setPolygonMode(bool isWire)
{
    makeCurrent();
    if(isWire)
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    doneCurrent();
    update();
}

void MyOpenglWidget::initializeGL()
{
    this->initializeOpenGLFunctions();
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);

    //将数据传入显存
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    //告知显卡如何解析
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(GLvoid*)0);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*sizeof(float),(GLvoid*)(3*sizeof(float)));
    glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*sizeof(float),(GLvoid*)(6*sizeof(float)));

    //开启VAO的属性
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    //EBO
    glGenBuffers(1,&EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);

    //链接 link shaders
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shaders/shapes.vert");
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shaders/shapes.frag");

    bool success=shaderProgram.link();
    if(!success)
        qDebug()<<shaderProgram.log();
    shaderProgram.bind();

    textureWall=new QOpenGLTexture(QImage(":/resources/wall.jpg").mirrored());
    textureContainer=new QOpenGLTexture(QImage(":/resources/awesomeface.png").mirrored());

    shaderProgram.setUniformValue("textureWall",0);
    shaderProgram.setUniformValue("textureContainer",1);
    //解绑
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,0);
}

void MyOpenglWidget::resizeGL(int w, int h)
{

}

void MyOpenglWidget::paintGL()
{
    glClearColor(0.2f,0.3f,0.3f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    if(!isDraw)
        return;
    shaderProgram.bind();

    glBindVertexArray(VAO);
    textureWall->bind(0);
    textureContainer->bind(1);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    //glDrawArrays(GL_TRIANGLES,0,6);
    glDrawElements(ShapeType,6,GL_UNSIGNED_INT,0);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glDrawElements(ShapeType,3,GL_UNSIGNED_INT,0);
    //解绑
    glBindVertexArray(0);
}

